package scala.pattern

/**
 * 偏函数
 */
object PartialFunctionTest1 {

  def main(args: Array[String]): Unit = {
    //检查输入参数是否合格
    val second = new PartialFunction[List[Int], Option[Int]] {
      override def isDefinedAt(x: List[Int]): Boolean = x match {
        case x :: y :: _ => true
        case _ => false
      }
      //执行函数逻辑
      override def apply(v1: List[Int]): Option[Int] = v1 match {
        case x:: y:: _=> Some(y)
      }
    }

    println(second.applyOrElse(List(1,3,4),(_:List[Int])=>None))
  }

}
